package cn.aion.dao;

import cn.aion.model.VideoData;
import org.apache.ibatis.annotations.*;

/**
 * 视频数据操作接口
 * Created by 虚空之翼 on 2016/11/8.
 */
public interface VideoDataDAO {
    @Insert("insert into ad_video_data (video_id,src_type,play_count,play_add,comments,scan_time) " +
            "values (#{data.videoId},#{data.srcType},#{data.playCount},#{data.playAdd},#{data.comments},unix_timestamp(now()))")
    @Options(useGeneratedKeys = true, keyProperty = "data.id")
    int save(@Param("data") VideoData data);

    @Delete("delete from ad_video_data where id=#{id}")
    int delete(@Param("id") Long id);

    @Update("update ad_video_data set play_count=#{data.playCount},play_add=#{data.playAdd}," +
            "comments=#{data.comments},scan_time=unix_timestamp(now()) where id=#{data.id}")
    int update(@Param("data") VideoData data);


    @Select("select * from ad_video_data where id=#{id}")
    VideoData selectOne(@Param("id") Long id);

    // 前一天的数据
    @Select("select * from ad_video_data where video_id=#{videoId} and src_type=#{srcType} " +
            "and DATE_FORMAT(FROM_UNIXTIME(`scan_time`),'%Y-%m-%d') = date_sub(curdate(),interval 1 day) order by id desc limit 1")
    VideoData selectOnePrevious(@Param("videoId") Long videoId, @Param("srcType") Integer srcType);

    // 当天的数据
    @Select("select * from ad_video_data where video_id=#{videoId} and src_type=#{srcType} " +
            "and DATE_FORMAT(FROM_UNIXTIME(`scan_time`),'%Y-%m-%d') = DATE_FORMAT(now(),'%Y-%m-%d') order by id desc limit 1")
    VideoData selectOneToday(@Param("videoId") Long videoId, @Param("srcType") Integer srcType);

}
